package com.zr.inquiry.total.controller;

import com.zr.inquiry.total.model.pojo.*;
import com.zr.inquiry.total.service.InquiryService;
import com.zr.inquiry.spare.model.pojo.QueryPageSelectMaterialVo;
import com.zr.inquiry.spare.service.SpareService;
import com.zr.utils.page.PageUtil;
import com.zr.utils.result.ResultBuilderUtil;
import com.zr.utils.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName InquiryController
 * @Description TODO 询价清单控制器
 * @Author 马家奎
 * @Date 2019/11/14 19:33
 */
@CrossOrigin
@RestController
@RequestMapping("inquiryList")
public class InquiryController {
    @Autowired
    private InquiryService inquiryService;
    @Autowired
    private SpareService spareService;
    /*
    * @Author 马家奎
    * @Description //TODO 分页查询询价清单 * @Date
    * @Param
    * @return
    **/
    @PostMapping("search")
    public ResultUtil queryPage(@RequestBody QueryPageInquiryVo pageInquiryVo){

        PageUtil<QueryPageInquiryResultVo> page = inquiryService.queryPage(pageInquiryVo);
        return ResultBuilderUtil.success(page);
    }
    /*
    * @Author 马家奎
    * @Description //TODO 添加询价清单 * @Date
    * @Param
    * @return
    **/
    @PostMapping("add")
    public ResultUtil add(@RequestBody @Valid InquiryAddVo addVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ResultBuilderUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }else {
            return inquiryService.add(addVo);
        }
    }
    /*
    * @Author 马家奎
    * @Description //TODO 修改询价清单 * @Date
    * @Param
    * @return
    **/
    @PostMapping("update")
    public ResultUtil update(@RequestBody @Valid InquiryUpdateVo updateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ResultBuilderUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }else {
            return inquiryService.update(updateVo);
        }
    }

    /*
    * @Author 马家奎
    * @Description //TODO 查询总备件库所有备件 * @Date
    * @Param
    * @return
    **/
    @PostMapping("selectMaterial")
    public ResultUtil selectMaterial(@RequestBody QueryPageSelectMaterialVo materialVo){

        PageUtil<QueryPageSelectMaterialVo> page = spareService.selectMaterial(materialVo);

        return ResultBuilderUtil.success(page);
    }
    /*
    * @Author 马家奎
    * @Description //TODO 作废 * @Date
    * @Param
    * @return
    **/
    @PostMapping("modifyState")
    public ResultUtil modifyState(@RequestBody @Valid ModifyStateVo modifyStateVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return  ResultBuilderUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return inquiryService.modifyState(modifyStateVo);
    }
}
